package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.entity.ShaneCollectAnswer;
import com.baizhi.shane.shane.vo.AnswerUserRelVO;
import com.baizhi.shane.sys.manager.RedisManager;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.entity.ShaneAgainstAnswer;
import com.baizhi.shane.shane.service.ShaneAgainstAnswerService;
import com.baizhi.shane.shane.mapper.ShaneAgainstAnswerMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author wbz12
* @description 针对表【shane_against_answer(啥呢回答反对表)】的数据库操作Service实现
* @createDate 2023-07-25 22:33:37
*/
@Service
public class ShaneAgainstAnswerServiceImpl extends ServiceImpl<ShaneAgainstAnswerMapper, ShaneAgainstAnswer>
    implements ShaneAgainstAnswerService{

    @Resource
    private RedisManager redisManager;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 用户反对回答，只需要answerId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 16:38 2023/10/12
     * @Param answerUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String insertOneAnswerAgainst(AnswerUserRelVO answerUserRelVO) {
        Long userId = redisManager.getUserId();
        Long answerId = answerUserRelVO.getAnswerId();
        ShaneAgainstAnswer shaneAgainstAnswer = new ShaneAgainstAnswer();
        shaneAgainstAnswer.setUserId(userId);
        shaneAgainstAnswer.setAnswerId(answerId);
        boolean save = this.save(shaneAgainstAnswer);
        String succeed = commonUtils.boolToString(save, SysMapping.AGAINST_SUCCESS, SysMapping.AGAINST_FAIL);
        return succeed;
    }

    /**
     * 用户取消反对回答，只需要answerId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 16:42 2023/10/12
     * @Param answerUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String deleteOneAnswerAgainst(AnswerUserRelVO answerUserRelVO) {
        Long userId = redisManager.getUserId();
        Long answerId = answerUserRelVO.getAnswerId();

        LambdaUpdateWrapper<ShaneAgainstAnswer> uwSaa = new LambdaUpdateWrapper<>();
        uwSaa.eq(ShaneAgainstAnswer::getUserId,userId);
        uwSaa.eq(ShaneAgainstAnswer::getAnswerId,answerId);
        uwSaa.eq(ShaneAgainstAnswer::getDelFlag,0);
        uwSaa.set(ShaneAgainstAnswer::getDelFlag, 1);
        uwSaa.set(ShaneAgainstAnswer::getDelBy, userId);
        uwSaa.set(ShaneAgainstAnswer::getDelTime, new Date());
        boolean update = this.update(uwSaa);
        String succeed = commonUtils.boolToString(update, SysMapping.AGAINST_CANCEL_SUCCESS, SysMapping.AGAINST_CANCEL_FAIL);
        return succeed;
    }

}




